package com.devframe.component.ftlprocess.meta;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.devframe.component.ftlprocess.FTLProcessGlobalConstants;
import com.devframe.component.ftlprocess.xml.parse.XmlTagAttr;
/**
 * 
 * @author 詹保山(softzbs@126.com)
 * Mar 15, 2012<br/>
 * @描述：使用SAX解析XML的DOM 结构，将XML转为对象模型
 */
public class Model extends DefaultHandler {


	/**
	 *  静态化变量 ID属性
	 */
	private static String XML_ATTR_ID=XmlTagAttr.CommonAttr.id.name();
	
	private static String XML_COLUMN_TAG=XmlTagAttr.ColumnAttr.column.name();
	private static String XML_COLUMN_ATTR_FIELD=XmlTagAttr.ColumnAttr.field.name();
	private static String XML_COLUMN_ATTR_LABEL=XmlTagAttr.ColumnAttr.label.name();
	
	private static String XML_SQL_TAG=XmlTagAttr.DataSetAttr.sql.name();
	private static String XML_HQL_TAG=XmlTagAttr.DataSetAttr.hql.name();
	
	private static String XML_BEAN_TAG=XmlTagAttr.DataSetAttr.bean.name();
	private static String XML_BEAN_ATTR_SERVICE=XmlTagAttr.BeanAttr.service.name();
	private static String XML_BEAN_ATTR_METHOD=XmlTagAttr.BeanAttr.method.name();
	
	
	
	private Map<String,String> columns;
	private List<String> columnKeys;
	private List<String> columnVals;
	
	private List<DataSet> datasets;
	
	private boolean isSQL=false;
	
	private boolean isHQL=false;
	private DataSet dataset;
	
	public Model(){
		this.columns=new HashMap<String, String>();
		this.columnKeys=new ArrayList<String>();
		this.columnVals=new ArrayList<String>();
		this.datasets=new ArrayList<DataSet>();
	}

	public void startElement(String uri,String localName,String qName,Attributes attributes) throws SAXException {
		//System.out.println("节点名称="+qName);
		
		if(qName.equals(Model.XML_COLUMN_TAG)){//COLUMN节点
			this.columnKeys.add(attributes.getValue(Model.XML_COLUMN_ATTR_FIELD));
			this.columnVals.add(attributes.getValue(Model.XML_COLUMN_ATTR_LABEL));
			columns.put(attributes.getValue(Model.XML_COLUMN_ATTR_FIELD), attributes.getValue(Model.XML_COLUMN_ATTR_LABEL));
		}else if(qName.equals(Model.XML_SQL_TAG)){//SQL节点
			isSQL=true;
			this.dataset=new DataSet(attributes.getValue(Model.XML_ATTR_ID),FTLProcessGlobalConstants.DATASET_TYPE_SQL);
			this.datasets.add(this.dataset);
		}else if(qName.equals(Model.XML_BEAN_TAG)){//bean 节点
			this.datasets.add(new DataSet(attributes.getValue(Model.XML_ATTR_ID),FTLProcessGlobalConstants.DATASET_TYPE_BEAN,attributes.getValue(Model.XML_BEAN_ATTR_SERVICE),attributes.getValue(XML_BEAN_ATTR_METHOD)));
		}else if(qName.equals(Model.XML_HQL_TAG)){//hql 节点
			isHQL=true;
			this.dataset=new DataSet(attributes.getValue(Model.XML_ATTR_ID),FTLProcessGlobalConstants.DATASET_TYPE_HQL);
			this.datasets.add(this.dataset);
		}
	}

	public void characters(char[] ch,int offset,int length) throws SAXException {
		if(isSQL){//当是SQL节点时须取内容
			this.dataset.setSql(this.dataset.getSql()+ new String(ch, offset, length));
		}else if(isHQL){//当是HQL节点时须取内容
			this.dataset.setHql(this.dataset.getHql()+ new String(ch, offset, length));
		}
	}
	

	public void endElement(String uri, String localName, String name)
			throws SAXException {
		isSQL=false;
		isHQL=false;
		this.dataset=null;
	}

	/**
	 * 
	 * Mar 15, 2012<br/>
	 * @描述：获取列的信息
	 * <br/> 
	 * @return
	 */
	public Map<String, String> getColumns() {
		return columns;
	}

	/**
	 * 
	 * Mar 15, 2012<br/>
	 * @描述：获取数据集
	 * <br/> 
	 * @return
	 */
	public List<DataSet> getDatasets() {
		return datasets;
	}
	/**
	 * 
	 * Mar 15, 2012<br/>
	 * @描述：销毁内建对象
	 * <br/>
	 */
	public void destory(){
		this.columnKeys.clear();
		this.columns.clear();
		this.columnKeys=null;
		this.columns=null;
		this.datasets.clear();
		this.datasets=null;
		
	}

	public void setColumns(Map<String, String> columns) {
		this.columns = columns;
	}

	public void setDatasets(List<DataSet> datasets) {
		this.datasets = datasets;
	}
	public String toString() {
		StringBuffer sb=new StringBuffer(32);
		sb.append("字段条目配置：clounms=【"+this.columns+"】");
		for(DataSet ds:this.datasets){
			sb.append("【"+ds+"】");
		}
		return sb.toString();
	}

	public List<String> getColumnKeys() {
		return columnKeys;
	}

	public List<String> getColumnVals() {
		return columnVals;
	}
	
}
